using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Text;
using ZoneFiveSoftware.Common.Data.Fitness;
using FilteredStatistics.Common;
using FilteredStatistics.Common.Controller;
using FilteredStatistics.Controller;
using FilteredStatistics.View;

namespace FilteredStatistics.Data
{
    class TemplateFilterCriteria : FilterCriteriaFacade
    {
        public TemplateFilterCriteria()
        {
            Activity = null;

            TemplateManager.Instance.PropertyChanged += new PropertyChangedEventHandler(OnTemplateManagerPropertyChanged);

            m_NamedZonesDirty = true;
            TriggerNamedZonesListChanged();
        }

        void OnTemplateManagerPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Templates")
            {
                m_NamedZonesDirty = true;
                TriggerNamedZonesListChanged();
            }
        }

#region FilterCriteriaFacade members

        public override Guid ReferenceId
        {
            get { return GUIDs.TemplateCriteriaGuid; }
        }

        public override IActivity Activity
        {
            set
            {
                if (m_Activity != value)
                {
                    m_Activity = value;

                    foreach (TemplateNamedZone namedZone in NamedZones)
                    {
                        namedZone.Activity = m_Activity;
                    }
                }
            }
        }

        public override String DisplayName
        {
            get { return PluginView.GetLocalizedString("TemplatesText"); }
        }

        public override List<object> NamedZones
        {
            get
            {
                if (m_NamedZonesDirty)
                {
                    RebuildNamedZonesList();
                }

                return m_NamedZones; 
            }
        }

        public override bool IsTemplateOnly
        {
            get { return false; }
        }

        public override bool IsMainViewOnly
        {
            get { return false; }
        }

        public override bool IsSerializable
        {
            get { return true; }
        }

        public override void SerializeCriteria(Stream stream)
        {
            Serialize(stream);
        }

        public override UInt16 DataVersion
        {
            get { return Constants.CurrentVersion.VersionNumber; }
        }

        public override object DeserializeCriteria(Stream stream, UInt16 version)
        {
            DataVersion dataVersion = new DataVersion(version);

            try
            {
                Deserialize(stream, dataVersion);
            }
            catch
            {
                return null;
            }

            return this;
        }

#endregion

#region IPluginSerializable members

        public override void Serialize(Stream stream)
        {
            base.Serialize(stream);
        }

#endregion

#region Deserialization methods

        public void Deserialize_V1(Stream stream, DataVersion version)
        {
        }

#endregion

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            TemplateFilterCriteria criteria = obj as TemplateFilterCriteria;

            if (criteria != null)
            {
                return true;
            }

            return base.Equals(obj);
        }

        private void RebuildNamedZonesList()
        {
            m_NamedZonesDirty = false;
            m_NamedZones.Clear();

            for (int i = 0; i < TemplateManager.Instance.GetTemplateCount(); ++i)
            {
                Template currentTemplate = TemplateManager.Instance.GetTemplate(i);
                TemplateNamedZone namedZone;

                namedZone = new TemplateNamedZone(m_Activity, currentTemplate);
                m_NamedZones.Add(namedZone);
            }

            if (TemplateManager.Instance.GetTemplateCount() < m_NamedZones.Count)
            {
                m_NamedZones.RemoveRange(TemplateManager.Instance.GetTemplateCount(), m_NamedZones.Count - TemplateManager.Instance.GetTemplateCount());
            }
        }

        IActivity m_Activity = null;
        private List<object> m_NamedZones = new List<object>();
        private bool m_NamedZonesDirty = false;
    }
}
